<template>
	<div class="app-container">
		<el-form size="small" :inline="true" label-width="68px">
			<el-form-item label="类型" prop="status">
				<el-select v-model="value" placeholder="请选择">
					<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索</el-button>
			</el-form-item>
		</el-form>

		<el-table border v-loading="loading" :data="list" show-summary>
			<el-table-column label="月份" align="center" prop="month" width="70" />
			<el-table-column label="被投诉次数" align="center" prop="total" width="100" />
			<el-table-column v-for="(goods, index) in goodsList" :key="index" :label="goods" align="center" :prop="goods"></el-table-column>
		</el-table>
	</div>
</template>

<script>
	import { getFeedback, getComplaintGoodsList } from "@/api/complaint/order"
	export default {
		data() {
			return {
				options: [{
					value: 1,
					label: '半年度'
				}, {
					value: 2,
					label: '年度'
				}],
				value: 2,
				startTime: '',
				endTime: '',
				loading: true,
				goodsList: [],
				list: []
			}
		},
		created() {
			this.getGoodsList();
		},
		methods: {
			/**
			 * 产品列表
			 */
			getGoodsList() {
				getComplaintGoodsList().then(res => {
					if (res.code == 200) {
						res.data.forEach(goods => {
							this.goodsList.push(goods.dictValue);
						});

						this.getList();
					}
				});
			},
			/**
			 * 列表
			 */
			getList() {
				this.list = [];
				this.loading = true;
				
				this.getTime();
				
				getFeedback(this.startTime, this.endTime).then(res => {
					let endNum = this.value == 1 ? 6 : 12;
					for (let i = 1; i <= endNum; i++) {
						let numList = [];
						let total = 0;
						
						this.goodsList.forEach(goods => {
							let num = '';
							res.data.forEach(item => {
								if (item.goodsName == goods && item.month == i) {
									num = item.num;
									total += num;
								}
							});

							numList.push(num);
						});

						let goodsList = this.goodsList.reduce((acc, key, index) => {
							acc[key] = numList[index];
							return acc;
						}, {});

						goodsList.month = i;
						goodsList.total = total;
						this.list.push(goodsList);
					}

					this.loading = false;
				});
			},
			/**
			 * 获取时间
			 */
			getTime(){
				let currentYear = new Date().getFullYear();
				this.startTime = `${currentYear}-01-01 00:00:00`
				
				if(this.value == 1){ // 半年
					this.endTime = `${currentYear}-06-30 23:59:59`
				}else{ // 全年
					this.endTime = `${currentYear}-12-31 23:59:59`
				}
			}
		}
	}
</script>

<style>
</style>